import random
import time

from ascript.android.node import Selector
from ascript.android import action
import re
from .utils import timeSleep, closeAdvertisingOnce, slide,heightPixels,clickGuangGao,guangGaoTime

# 滚动元素
def elementVisible():
    print('进入滚动获取元素')
    while True:
        element = Selector().path("/FrameLayout/FrameLayout/LynxFlattenUI").find_all()
        if element == None:
            continue
        print('元素个数',len(element))
        # 点击广告
        clickGuangGao()
        # 关闭广告
        closeAdvertisingOnce()
        for itm in element:
            if '立即翻卡' in itm.text:
                print(55555)
            if '立即翻卡' in itm.text and itm.visible==True and itm.center_y<int(heightPixels/2):
                print('元素成功显示',itm)
                return itm
        slide(num=100)
        timeSleep()

# 吃饭补贴
def fanKaLingqu11():
    z = 0
    while True:
        element = Selector().path("/FrameLayout/FrameLayout/LynxFlattenUI").find_all()
        if element is None:
            timeSleep()
            continue
        z += 1
        for itm in element:
            center_x = itm.center_x
            center_y = itm.center_y
            if '立即翻卡' in itm.text:
                z = 0
                # 判断元素是否在可见区域
                if itm.visible==False:
                    # 不在可见区域则滑动到可见区域
                    visible = elementVisible()
                    center_x = visible.center_x
                    center_y = visible.center_y
                # 点击补贴
                print('点击翻卡')
                timeSleep()
                action.click(x=center_x, y=center_y, dur=random.randint(20, 500))
                i = 0
                while True:
                    i +=1
                    click = clickGuangGao()
                    if click == True:
                        i = 0
                        # 获取广告的时长
                        guangGaoTime()
                    close1 = closeAdvertisingOnce()
                    if close1==True:
                        i = 0
                    if i>=10:
                        print('翻卡退出')
                        break
                    timeSleep()
        if z>15:
            break



def fanKaLingqu():
    for i in range(10):
        while True:
            element = Selector().path("/FrameLayout/FrameLayout/LynxFlattenUI").find_all()
            timeSleep()
            if element:
                break
        for itm in element:
                center_x = itm.center_x
                center_y = itm.center_y
                if '立即翻卡' in itm.text:
                    z = 0
                    # 判断元素是否在可见区域
                    if itm.visible == False:
                        # 不在可见区域则滑动到可见区域
                        visible = elementVisible()
                        center_x = visible.center_x
                        center_y = visible.center_y
                    print('点击翻卡')
                    timeSleep()
                    action.click(x=center_x, y=center_y, dur=random.randint(20, 500))
                    for itm in range(10):
                        # 等待时间
                        timeSleep()
                        # 点击广告
                        print('翻卡=================点击广告' +str(itm))
                        clickGuangGao()
                        # 关闭广告
                        print('翻卡=================点击关闭广告')
                        closeAdvertisingOnce()
                        timeSleep()
                        fuli = Selector().id("com.phoenix.read:id/acv").find()
                        if fuli:
                            print('退出返回主页')
                            return